package com.haiqi.modules.map.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.Server;
import com.haiqi.common.persistence.entity.ServerPoint;
import com.haiqi.common.utils.LonLatUtils;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.map.service.ServerPointService;
import com.haiqi.modules.map.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/5 0005.
 */
@RequestMapping("/manage/map/server-point")
@Controller
@SafeAuthority(menuCode = "monitorLocation,companyMonitorLocation")
public class ServerPointController extends BaseController {
    @Autowired
    private ServerPointService serverPointService;

    @Autowired
    private ServerService serverService;

    private String menuCode;
    @RequestMapping("/save")
    @ResponseBody
    public Map save(ServerPoint serverPoint, String wkt, HttpSession session){
        Map map = new HashMap();
        ResponseBean.UserBean  user = getUser(session);
        serverPoint.setCoordinates(wkt);
        serverPoint.setCompany(user.getOfficeid());
        serverPoint.setCompanyName(user.getName());
        String keyword = serverPoint.getKeyword();
        if(keyword!=null)
        {
            keyword=keyword.replace('，',',');
            String[] arr = keyword.split(",");
            if(arr.length > 3){
                map.put("result", false);
                map.put("keyword",false);
                return map;
            }else {
                for (String s : arr) {
                    if (s.length() > 5) {
                        map.put("result", false);
                        map.put("keyword", false);
                        return map;
                    }
                }
            }
        }
        boolean success = serverPointService.save(serverPoint);
        map.put("result",success);
        map.put("id",serverPoint.getId());
        return map;
    }

    @RequestMapping("/list")
    public String list(ServerPoint serverPoint, PageBean pageBean, HttpSession session, Model model,String key,Server server) {
        ResponseBean.UserBean user = getUser(session);
        serverPoint.setCompany(user.getOfficeid());
        serverPoint.setName(key);
        serverPoint.setKeyword(key);
        menuCode = "servicePoint";
        generateMenuCode(session, menuCode);
        PageInfo<ServerPoint> pageInfo = serverPointService.getDataList(serverPoint, pageBean);
        for (ServerPoint point : pageInfo.getList()) {
           String[] str=point.getWkt().substring(6,point.getWkt().length()-1).split(" ");
            String lon=LonLatUtils.changeLon(Double.parseDouble(str[0]));
            String lat=LonLatUtils.changeLat(Double.parseDouble(str[1]));
            String lonLat="["+lon+","+lat+"]";
           point.setWkt(lonLat);
        }
        model.addAttribute("param", serverPoint);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("key", key);
        return "/modules/service/publish-point";
    }


    @RequestMapping("/search-point")
    public String searchPoint(ServerPoint serverPoint, PageBean pageBean, HttpSession session, Model model,String key) {
          menuCode = "serviceSearch";
          serverPoint.setName(key);
          serverPoint.setKeyword(key);
          serverPoint.setStatus("1");
          generateMenuCode(session, menuCode);
          PageInfo<ServerPoint> pageInfo = serverPointService.getDataList(serverPoint, pageBean);
            for (ServerPoint point : pageInfo.getList()) {
                String[] str=point.getWkt().substring(6,point.getWkt().length()-1).split(" ");
                String lon=LonLatUtils.changeLon(Double.parseDouble(str[0]));
                String lat=LonLatUtils.changeLat(Double.parseDouble(str[1]));
                String lonLat="["+lon+","+lat+"]";
                point.setWkt(lonLat);
            }
          model.addAttribute("param", serverPoint);
          model.addAttribute("pageInfo", pageInfo);
          model.addAttribute("key", key);
        return "/modules/service/search-point";

    }



    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(ServerPoint serverPoint){
        boolean  success = serverPointService.delete(serverPoint);
        Map map = new HashMap();
        map.put("result",success);
        return map;
    }

    @ResponseBody
    @RequestMapping("/update")
    public Map update(ServerPoint serverPoint){
        boolean  success = serverPointService.update(serverPoint);
        Map map = new HashMap();
        map.put("result",success);
        return map;
    }


    @ResponseBody
    @RequestMapping("/ajax/getById")
    public ServerPoint getById(String id){
        ServerPoint  serverPoint = serverPointService.selectByPrimaryKey(id);
        return serverPoint;
    }

    @ResponseBody
    @RequestMapping("/ajax/data/list")
    public List<ServerPoint> getDataList(ServerPoint serverPoint){
//        ResponseBean.UserBean  user = getUser(session);
//        serverPoint.setCompany(user.getOfficeid());
        List<ServerPoint> dataList = serverPointService.getDataList(serverPoint);
        return dataList;
    }

    @ResponseBody
    @RequestMapping("/ajax/check")
    public List<ServerPoint> selectServerPointCheck(ServerPoint serverPoint){
        List<ServerPoint> serverPointList= serverPointService.selectCheck(serverPoint);
        return serverPointList;
    }



    @ResponseBody
    @RequestMapping("/ajax/data/page/list")
    public DataGridBean<ServerPoint> getPageDataList(ServerPoint serverPoint, PageBean page,String key){
        serverPoint.setKeyword(key);
        serverPoint.setName(key);
        PageUtils.generatePage(page);
        PageInfo<ServerPoint> pageInfo= serverPointService.selectServerPointCheck(serverPoint,page);
        DataGridBean<ServerPoint> mobileShipDataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo, mobileShipDataGridBean);
        return mobileShipDataGridBean;
    }
}
